﻿//
// Created by work on 24-6-6.
//

#include "CRtDeviceInfo.h"

std::map<CRtDeviceInfo::EDeviceCol, QString> CRtDeviceInfo::rtDeviceColToStringMap = {
    {CRtDeviceInfo::EDeviceCol::EUser, "userid"},
    {CRtDeviceInfo::EDeviceCol::EName, "name"},
    {CRtDeviceInfo::EDeviceCol::EModelName, "model_name"},
    {CRtDeviceInfo::EDeviceCol::EDescr, "descr"},
    {CRtDeviceInfo::EDeviceCol::ESn, "sn"},
    {CRtDeviceInfo::EDeviceCol::EGroupIngs, "groupings"},
    {CRtDeviceInfo::EDeviceCol::EConfigs, "configs"},
    {CRtDeviceInfo::EDeviceCol::ELocation, "location"},
    {CRtDeviceInfo::EDeviceCol::ECreate_time, "create_time"},
    {CRtDeviceInfo::EDeviceCol::EProtocol, "protocol"},
    {CRtDeviceInfo::EDeviceCol::EFirmwareVersion, "firmware_version"},
    {CRtDeviceInfo::EDeviceCol::ELastOnlineTime, "last_online_time"},
    {CRtDeviceInfo::EDeviceCol::EAccruedOnlineTime, "accrued_online_time"},
    {CRtDeviceInfo::EDeviceCol::ESpecifications, "specifications"},
    {CRtDeviceInfo::EDeviceCol::EValid, "valid"},

    {CRtDeviceInfo::EDeviceCol::EPulse, "pulse"},
    {CRtDeviceInfo::EDeviceCol::ELoginTime, "login_time"},
    {CRtDeviceInfo::EDeviceCol::EOnlineState, "online_state"}
};

CRtDeviceInfo::CRtDeviceInfo() {
    create_time = QDateTime::currentDateTime();
    last_online_time = QDateTime::currentDateTime();
    login_time = QDateTime::currentDateTime();
}

QJsonObject CRtDeviceInfo::toJson() {
    QJsonObject json;
    json[deviceFieldName(DEVICE_ENUM::EModelName)] = model_name;
    json[deviceFieldName(DEVICE_ENUM::EUser)] = user;
    json[deviceFieldName(DEVICE_ENUM::EName)] = name;
    json[deviceFieldName(DEVICE_ENUM::EDescr)] = descr;
    json[deviceFieldName(DEVICE_ENUM::ESn)] = sn;
    json[deviceFieldName(DEVICE_ENUM::ELocation)] = location;
    json[deviceFieldName(DEVICE_ENUM::EGroupIngs)] = grouping;
    json[deviceFieldName(DEVICE_ENUM::EConfigs)] = configs;
    json[deviceFieldName(DEVICE_ENUM::EProtocol)] = protocol;
    json[deviceFieldName(DEVICE_ENUM::EFirmwareVersion)] = firmware_version;
    json[deviceFieldName(DEVICE_ENUM::ELastOnlineTime)] = last_online_time.toString("yyyy-MM-dd hh:mm:ss");
    json[deviceFieldName(DEVICE_ENUM::ECreate_time)] = create_time.toString("yyyy-MM-dd hh:mm:ss");
    json[deviceFieldName(DEVICE_ENUM::EAccruedOnlineTime)] = accrued_online_time;
    json[deviceFieldName(DEVICE_ENUM::ESpecifications)] = specifications;
    json[deviceFieldName(DEVICE_ENUM::EValid)] = valid;
    json[deviceFieldName(DEVICE_ENUM::EPulse)] = pulse;
    json[deviceFieldName(DEVICE_ENUM::ELoginTime)] = login_time.toString("yyyy-MM-dd hh:mm:ss");
    json[deviceFieldName(DEVICE_ENUM::EOnlineState)] = online_state;
    return json;
}

void CRtDeviceInfo::fromJson(QJsonObject json) {
    this->user = json[deviceFieldName(EDeviceCol::EUser)].toString();
    this->name = json[deviceFieldName(EDeviceCol::EName)].toString();
    this->model_name = json[deviceFieldName(EDeviceCol::EModelName)].toString();
    this->descr = json[deviceFieldName(EDeviceCol::EDescr)].toString();
    this->sn = json[deviceFieldName(EDeviceCol::ESn)].toInt();
    this->grouping = json[deviceFieldName(EDeviceCol::EGroupIngs)].toString();
    this->configs = json[deviceFieldName(EDeviceCol::EConfigs)].toObject();
    this->location = json[deviceFieldName(EDeviceCol::ELocation)].toString();
    this->create_time = QDateTime::fromString(json[deviceFieldName(EDeviceCol::ECreate_time)].toString(), "yyyy-MM-dd hh:mm:ss");
    this->protocol = json[deviceFieldName(EDeviceCol::EProtocol)].toString();
    this->firmware_version = json[deviceFieldName(EDeviceCol::EFirmwareVersion)].toString();
    this->last_online_time = QDateTime::fromString(json[deviceFieldName(EDeviceCol::ELastOnlineTime)].toString(), "yyyy-MM-dd hh:mm:ss");
    this->accrued_online_time = json[deviceFieldName(EDeviceCol::EAccruedOnlineTime)].toInt();
    this->specifications = json[deviceFieldName(EDeviceCol::ESpecifications)].toString();
    this->valid = json[deviceFieldName(EDeviceCol::EValid)].toBool(true);
    this->pulse = json[deviceFieldName(EDeviceCol::EPulse)].toInt();
    this->login_time = QDateTime::fromString(json[deviceFieldName(EDeviceCol::ELoginTime)].toString(), "yyyy-MM-dd hh:mm:ss");
    this->online_state = json[deviceFieldName(EDeviceCol::EOnlineState)].toBool(true);
}
